package com.admin.controller;

import com.admin.vo.Credentials;
import com.admin.vo.TokenInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@Slf4j
public class LoginController {

  @Autowired
  private RestTemplate restTemplate;

  @PostMapping("login")
  public void login(@RequestBody Credentials credentials, HttpServletRequest request){
    log.info("111111");
    String oauthServiceUrl="http://admin.test.com:8091/token/oauth/token";
    HttpHeaders httpHeaders=new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    httpHeaders.setBasicAuth("admin","123456");

    MultiValueMap<String,String> params=new LinkedMultiValueMap<String,String>();
    params.add("username",credentials.getUsername());
    params.add("password",credentials.getPassword());
    params.add("grant_type","password");
    params.add("scope","read write");

    HttpEntity<MultiValueMap> httpEntity=new HttpEntity<MultiValueMap>(params,httpHeaders);

    ResponseEntity<TokenInfo> response = restTemplate.exchange(oauthServiceUrl, HttpMethod.POST, httpEntity, TokenInfo.class);
    log.info("token:{}",response.getBody());

    request.getSession().setAttribute("token",response.getBody());

  }

  @PostMapping("logout")
  public void logout(HttpServletRequest request){
    request.getSession().invalidate();
  }

  @GetMapping("/oauth/callback")
  public void callback(@RequestParam String code, String status, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String oauthServiceUrl="http://gateway.test.com:8091/token/oauth/token";
    HttpHeaders httpHeaders=new HttpHeaders();
    httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    httpHeaders.setBasicAuth("admin","123456");

    MultiValueMap<String,String> params=new LinkedMultiValueMap<String,String>();
    params.add("code",code);
    params.add("grant_type","authorization_code");
    params.add("redirect_uri","http://admin.test.com:8080/oauth/callback");

    HttpEntity<MultiValueMap> httpEntity=new HttpEntity<MultiValueMap>(params,httpHeaders);

    ResponseEntity<TokenInfo> token = restTemplate.exchange(oauthServiceUrl, HttpMethod.POST, httpEntity, TokenInfo.class);
    log.info("token:{}",token.getBody());

//    request.getSession().setAttribute("token",token.getBody().init());
    Cookie access_cookie=new Cookie("access_token",token.getBody().getAccess_token());
    access_cookie.setMaxAge(token.getBody().getExpires_in().intValue());
    access_cookie.setPath("/");
    access_cookie.setDomain("test.com");
    response.addCookie(access_cookie);
    Cookie refresh_token_cookie=new Cookie("refresh_token",token.getBody().getRefresh_token());
    refresh_token_cookie.setMaxAge(2592000);
    refresh_token_cookie.setPath("/");
    refresh_token_cookie.setDomain("test.com");
    response.addCookie(refresh_token_cookie);
    response.sendRedirect("/");

  }

  @GetMapping("me")
  public TokenInfo tokenInfo(HttpServletRequest request){
    TokenInfo token = (TokenInfo) request.getSession().getAttribute("token");
    return token;
  }
}
